﻿
namespace Collaboris.Utils
{
    using System;
    using System.IO;
    using System.Text;

    public class FileHelper
    {
        /// <summary>
        /// CopyFolders
        /// Works like XCopy. This copies a whole folder and it's contents to a different location.
        /// </summary>
        /// <param name="source">Folder to copy</param>
        /// <param name="destination">Location to copy the folder to</param>
        public static void CopyFolders(string source, string destination)
        {
            if (String.IsNullOrEmpty(source)) throw new ArgumentNullException("source");
            if (String.IsNullOrEmpty(destination)) throw new ArgumentNullException("destination");

            DirectoryInfo di = new DirectoryInfo(source);

            CopyFiles(source, destination);

            foreach (DirectoryInfo d in di.GetDirectories())
            {
                string newDir = Path.Combine(destination, d.Name);
                if (Directory.Exists(newDir))
                {
                    Directory.Delete(newDir, true);
                }

                Directory.CreateDirectory(newDir);

                CopyFolders(d.FullName, newDir);
            }
        }

        /// <summary>
        /// CopyFiles
        /// Copys all the files in the source folder to the destination one
        /// </summary>
        /// <param name="source">Folder to copy the files from</param>
        /// <param name="destination">Folder to copy the files to</param>
        public static void CopyFiles(string source, string destination)
        {
            if (String.IsNullOrEmpty(source)) throw new ArgumentNullException("source");
            if (String.IsNullOrEmpty(destination)) throw new ArgumentNullException("destination");

            DirectoryInfo di = new DirectoryInfo(source);
            FileInfo[] files = di.GetFiles();

            foreach (FileInfo f in files)
            {
                string sourceFile = f.FullName;
                string destFile = Path.Combine(destination, f.Name);
                if (File.Exists(destFile))
                    File.Delete(destFile);

                File.Copy(sourceFile, destFile, true);
            }
        }

        /// <summary>
        /// Writes the file.
        /// </summary>
        /// <param name="fileContents">The contents of the XML.</param>
        /// <param name="filename">The filename.</param>
        public static void WriteFile(string fileContents, string filename)
        {
            if (File.Exists(filename))
                File.Delete(filename);

            string containingDir = Path.GetDirectoryName(filename);
            if (!Directory.Exists(containingDir))
                Directory.CreateDirectory(containingDir);

            using (FileStream fs = File.Create(filename))
            {
                Byte[] info = new UTF8Encoding(true).GetBytes(fileContents);

                // Add the merged templated...
                fs.Write(info, 0, info.Length);
            }
        }

        /// <summary>
        /// Deletes the file.
        /// </summary>
        /// <param name="file">The file.</param>
        public static void DeleteFile(string file)
        {
            if (File.Exists(file))
                File.Delete(file);
        }

        public static void DeleteFileSystemInfo(FileSystemInfo fsi)
        {
            fsi.Attributes = FileAttributes.Normal;
            var di = fsi as DirectoryInfo;

            if (di != null)
                foreach (var dirInfo in di.GetFileSystemInfos())
                    DeleteFileSystemInfo(dirInfo);

            fsi.Delete();
        } 

    }
}
